﻿using System;

namespace codeparser.net.v2.Entities
{
    /// <summary>
    /// Represents a text tree node.
    /// </summary>
    internal class TreeNodeText : ITreeNode
    {
        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public string Value
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the parent node.
        /// </summary>
        /// <value>The parent node.</value>
        public ITreeNode Parent
        {
            get;
            set;
        }

        /// <summary>
        /// Returns a string representation of the current node.
        /// </summary>
        /// <returns>A string representation.</returns>
        public override string ToString()
        {
            // Check whether the text of this node is empty. If so, return an empty string.
            if (String.IsNullOrEmpty(this.Value))
            {
                return "";
            }

            // Get the content style.
            TreeNodeTag parentNode = this.Parent as TreeNodeTag;
            TagContentStyle contentStyle = TagContentStyle.Parse;
            if (parentNode != null && parentNode.Tag != null)
            {
                contentStyle = parentNode.Tag.ContentStyle;
            }

            return parentNode.TagConfiguration.ContentTransformer.Transform(this.Value, contentStyle);
        }

        /// <summary>
        /// Initializes a new instance of the TreeNodeText type.
        /// </summary>
        /// <param name="parent">The parent node.</param>
        /// <param name="value">The value.</param>
        public TreeNodeText(ITreeNode parent, string value)
        {
            // Initialize the fields.
            this.Parent = parent;
            this.Value = value;
        }
    }
}